from typing_extensions import TypedDict
from typing import List,Any,Dict

from typing_extensions import Annotated

def replace(left, right):
    return right

def deep_update(target, update):
    """
    递归更新目标字典
    :param target: 目标字典(将被更新)
    :param update: 提供更新内容的字典
    """
    for key, value in update.items():
        # 如果两个值都是字典，则递归更新
        if (key in target and isinstance(target[key], dict) 
                and isinstance(value, dict)):
            deep_update(target[key], value)
        else:
            target[key] = value
    return target



class AgentState(TypedDict):
    code: Annotated[int, replace]
    err_msg: Annotated[str, replace]
    debug: Annotated[bool, replace]
    log_id: Annotated[str, replace]
    session_id: Annotated[str, replace]
    user_id: Annotated[str, replace]
    stream: Annotated[bool, replace]
    is_delta: Annotated[bool, replace]
    messages: Annotated[List[Dict[str, Any]], replace]
    short_term_memory: Annotated[Dict[str, Any], deep_update]
    long_term_memory: Annotated[Dict[str, Any], deep_update]
    params: Annotated[Dict[str, Any], deep_update]
    extra: Annotated[Dict[str, Any], deep_update]
    node_context: Annotated[Dict[str, Any], deep_update]
    parallel_info: Annotated[Dict[str, Any], deep_update]

